#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    int pid = 0;
    int i = 0;
    
    if( (pid = fork()) > 0 ) {
        printf("parrent = %d, ppid = %d, pgid = %d, sid = %d\n", 
                    getpid(), getppid(), getpgrp(), getsid(getpid()));
        printf("new: %d\n", pid);
        exit(0);
    } else if( pid == 0 ) {
        setsid();

        sleep(60);

        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);

        i += open(argv[1], O_RDONLY); // 0 -- STD_IN
        i += open(argv[1], O_WRONLY); // 1 -- STD_OUT
        i += open(argv[1], O_RDWR); // 2 -- STD_ERR

        printf("child = %d, ppid = %d, pgid = %d, sid = %d\n", 
                    getpid(), getppid(), getpgrp(), getsid(getpid()));
        printf("i = %d\n", i);
    } else {
        printf("fork error...\n");
    }  
    
    sleep(240);
    
    return 0;
}
